package net.tv90.auth.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import jakarta.annotation.Resource;
import net.tv90.auth.config.properties.EncryptProperties;
import net.tv90.auth.utils.encrypt.DaoEncryptService;
import net.tv90.auth.utils.encrypt.AESDaoEncryptService;
import net.tv90.auth.utils.encrypt.Base64DaoEncryptService;
import net.tv90.auth.utils.encrypt.EncryptTypeHandler;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * MybatisPlus配置
 *
 * @author TV90
 */
@Configuration
@MapperScan("net.tv90.auth.dao")
@EnableConfigurationProperties({EncryptProperties.class})
public class MybatisPlusConfig {

    @Resource
    private EncryptProperties encryptProperties;

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        //分页插件
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }

    @Bean
    @ConditionalOnMissingBean(DaoEncryptService.class)
    public DaoEncryptService encryptService() {
        EncryptProperties.Algorithm algorithm = encryptProperties.getAlgorithm();
        DaoEncryptService encryptService;
        switch (algorithm) {
            case BASE64:
                encryptService = new Base64DaoEncryptService();
                break;
            case AES:
                encryptService = new AESDaoEncryptService();
                break;
            default:
                encryptService = null;
        }
        return encryptService;
    }

    @Bean
    public EncryptTypeHandler encryptTypeHandler() {
        return new EncryptTypeHandler();
    }
}